import java.awt.*;
import java.util.LinkedList;

public class DoraemonObjectManager {
/*    *//**
     * DoraemonObjectManager的单例模式
     *//*
    public static DoraemonObjectManager instance = new DoraemonObjectManager();*/
    public   LinkedList<AbstractBiology> abstractBiologyLinked = new LinkedList<AbstractBiology>();
    DrawAllDoraemonPhotoAndInfo drawAllDoraemonPhotoAndInfo = new DrawAllDoraemonPhotoAndInfo();
    public void render(Graphics g, Maps maps, MedusaObjectManager medusaObjectManager) {
        g.setColor(Color.red);
        for (int i = 0; i < abstractBiologyLinked.size(); i++) {
            AbstractBiology tempObject = abstractBiologyLinked.get(i);
            tempObject.render(g, maps, this, medusaObjectManager);
        }
        drawAllDoraemonPhotoAndInfo.render(g,abstractBiologyLinked);
    }

    public void tick() {
        for (int i = 0; i < abstractBiologyLinked.size(); i++) {
            AbstractBiology tempObject = abstractBiologyLinked.get(i);
            tempObject.tick();
        }
        isPlayerDeath();
        getCurrentPlayer();
    }

    public void playerMove(java.awt.event.MouseEvent e, GameStats gameStats, DoraemonObjectManager doraemonObjectManager, MedusaObjectManager medusaObjectManager, Maps maps) {
        for (int i = 0; i < abstractBiologyLinked.size(); i++) {
            AbstractBiology tempObject = abstractBiologyLinked.get(i);
            tempObject.playMove(e, gameStats, doraemonObjectManager, medusaObjectManager, maps);
        }
    }

    public void isPlayerDeath() {
        for (int i = 0; i < abstractBiologyLinked.size(); i++) {
            if (abstractBiologyLinked.get(i).getHP() <= 0) {
                abstractBiologyLinked.remove(i);
                //JOptionPane.showMessageDialog(null,"Player Death");
            }
        }
    }

    public boolean isHavePlayerBeingSelected() {
        for (int i = 0; i < abstractBiologyLinked.size(); i++) {
            if (abstractBiologyLinked.get(i).isSelected() || abstractBiologyLinked.get(i).isAttack() || abstractBiologyLinked.get(i).isMove()) {
                return true;
            }
        }
        return false;
    }

    public void selectPlayer(java.awt.event.MouseEvent e, Maps maps) {
        if (e.getX() / Global.CELL_SIZE == abstractBiologyLinked.get(getcurrentplayer).getY() + maps.offsetX / Global.CELL_SIZE &&
                e.getY() / Global.CELL_SIZE == abstractBiologyLinked.get(getcurrentplayer).getX() + maps.offsetY / Global.CELL_SIZE) {
            if (abstractBiologyLinked.get(getcurrentplayer).isSelected()) {
                abstractBiologyLinked.get(getcurrentplayer).setSelected(false);
            } else {
                abstractBiologyLinked.get(getcurrentplayer).setMove(false);
                abstractBiologyLinked.get(getcurrentplayer).setAttack(false);
                abstractBiologyLinked.get(getcurrentplayer).setSelected(true);
            }
        } else {
            for (int i = 0; i < abstractBiologyLinked.size(); i++) {
                if (e.getX() / Global.CELL_SIZE == abstractBiologyLinked.get(i).getY() + maps.offsetX / Global.CELL_SIZE &&
                        e.getY() / Global.CELL_SIZE == abstractBiologyLinked.get(i).getX() + maps.offsetY / Global.CELL_SIZE) {
                    for (int z = 0; z < abstractBiologyLinked.size(); z++) {
                        abstractBiologyLinked.get(z).setSelected(false);
                        abstractBiologyLinked.get(z).setMove(false);
                        abstractBiologyLinked.get(z).setAttack(false);
                    }
                    getcurrentplayer = i;
                    abstractBiologyLinked.get(getcurrentplayer).setSelected(true);
                    break;
                }
            }
        }

        drawAllDoraemonPhotoAndInfo.mousePressedPhoto(e,abstractBiologyLinked);
    }

    public boolean isHaveplayerInHereMouseE(java.awt.event.MouseEvent e, Maps maps) {
        for (int i = 0; i < abstractBiologyLinked.size(); i++) {
            if (e.getY() / Global.CELL_SIZE == abstractBiologyLinked.get(i).getX() + maps.offsetY &&
                    e.getX() / Global.CELL_SIZE == abstractBiologyLinked.get(i).getY() + maps.offsetX) {
                return true;
            }
        }
        return false;
    }

    public boolean isHaveplayerInHere(int x, int y) {
        for (int i = 0; i < abstractBiologyLinked.size(); i++) {
            if (x == abstractBiologyLinked.get(i).getX() && y == abstractBiologyLinked.get(i).getY()) {
                return true;
            }
        }
        return false;
    }

    public int getcurrentplayer = 0;

    public void getCurrentPlayer() {
        for (int i = 0; i < abstractBiologyLinked.size(); i++) {
            if (abstractBiologyLinked.get(i).isSelected()) {
                getcurrentplayer = i;
            }
        }
    }

    public void addPlayer(AbstractBiology object) {
        this.abstractBiologyLinked.add(object);
    }

    public void removePlayer(AbstractBiology object) {
        this.abstractBiologyLinked.remove(object);
    }
}

